import { GetNotice, AddNoticeLook } from '../../utils/request/api';
import User, { UserInfo } from "../../utils/user";
var user = new User;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    info: user.getInfo() as UserInfo,
    list: [] as any,
    user: user,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function () {
    await GetNotice({ class_id: this.data.info.class_id, s_code: this.data.info.code })
      .then(async (res: any) => {
        this.setData({
          list: res.data.data,
        })
      });
  },

  //返回主页
  onClickLeft() {
    wx.navigateBack();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: async function () {
    await GetNotice({ class_id: this.data.info.class_id, s_code: this.data.info.code })
      .then(async (res: any) => {
        this.setData({
          list: res.data.data,
        })
      });
  },

  //点击列表跳转
  onClickList(e: any) {
    const index: number = e.currentTarget.dataset.index;
    const item: any = this.data.list[index];
    this.trigger(item);
    console.log(item);
    const data: string = JSON.stringify(item);
    if (!item.islook) {
      AddNoticeLook({ n_id: item.id, s_code: this.data.info.code });
    }
    wx.navigateTo({
      url: '../../pages/notice/noticedetail/noticedetail?index=' + index + '&data=' + data
    });
  },

  trigger(value: any) {
    this.triggerEvent('listOutput', value);
  },
  
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})